<template>
  <article class="page">
    <headerbar>评价</headerbar>
    <section class="frame-content-sub no-bg">
      <ul class="top-menu">
        <li @click="peixunjigou()" :class="{'active':tabIndex == 0}"><span>培训机构</span></li>
        <li @click="zhubandanwei()" :class="{'active':tabIndex == 1}"><span>主办单位</span></li>
      </ul>
      <section class="block pdv-3 pdh-3" v-if="tabIndex == 0">

        <div class="score-item" v-for="item in organization">
          <div class="text-wrap">
            <div class="title">{{item.userName}}</div>
            <div class="tip">{{item.groupName}}</div>
          </div>
          <div class="input-wrap">
            <input v-model="item.value" maxlength="3" placeholder="请输入评分" type="text">
          </div>
        </div>

      </section>
      <section class="block pdv-3 pdh-3" v-if="tabIndex == 1">

        <div class="score-item" v-for="item in organizer">
          <div class="text-wrap">
            <div class="title">{{item.userName}}</div>
            <div class="tip">{{item.groupName}}</div>
          </div>
          <div class="input-wrap">
            <input v-model="item.value" maxlength="3" placeholder="请输入评分" type="text">
          </div>
        </div>

      </section>


      <div class="pdv-3"></div>
      <div @click.stop="saveData()" class="default-block-btn">提交评价</div>
      <div class="pdv-2"></div>

    </section>
  </article>
</template>

<script>
  import tools from '@/utils/tools'
  import appraiseInput from '@/components/dangjian/home/appraiseInput'
  import _ from 'lodash'
  export default {
    name: 'appraiseManage',
    components: {appraiseInput},
    data () {
      return {
        routeName: 'organization',
        pageIndex: 1,
        pageSize: 10,
        list: [],
        options: {
          pullUpLoad: true,
          scrollbar: false
        },
        id: '',
        userId: '',
        detail: {},
        organization: [],
        organizer: [],
        appraiseDialogInstance: null,
        data: {},
        tabIndex: 0
      }
    },
    created () {
      this.detail = this.$store.state.appraiseDetail
      this.userId = this.$store.state.userInfo.id
      this.id = this.$route.query.id
      // this.clickNav('organization')
      this.loadData()
    },
    methods: {
      // clickNav (routeName) {
      //   let query = _.extend(this.$route.query, {routeName: routeName})
      //   if (this.$route.query.routeName !== routeName) {
      //     this.$router.replace({name: this.$route.name, query: query})
      //   }
      //   this.routeName = routeName
      //   this.pageIndex = 1
      //   this.list = []
      //   this.organization(routeName)
      //   this.classList()
      // },
      submitAppraise (baseParams, data) {
        let params = []
        let _data = _.cloneDeep(data)
        _data.forEach(v => {
          v.score = v.value
          v.evaluationCriterionXmbm = v.xmbm
          v.evaluationCriterionPfxm = v.pfxm
          let temp = _.extend({}, baseParams, v)
          delete temp.id
          params.push(temp)
        })
        console.log('appraiseTeacher data', data)
        console.log('appraiseTeacher', params)
        this.fetchService.trainingplanpjSaveOrUpdateOfAll(params).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || '获取数据失败')
          }
          return this.tools.alert(res.message || '获取数据失败')
        })
      },
      classList () {
        let params = {
          classId: this.detail.classId,
          limit: 10000,
          page: 1
        }
        this.fetchService.classList(params).then(res => {
          if (res.code !== 0) {
            return this.tools.tip(res.message || '获取数据失败')
          }
          this.list = res.data
        })
      },
      peixunjigou () {
        this.tabIndex = 0
      },
      zhubandanwei () {
        this.tabIndex = 1
      },
      loadData () {
        this.data = []
        let params = {
          'id': this.id
        }
        this.fetchService.trainingplanClassSelectAll(params).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || res.resp_msg)
          }
          this.isLoading = false
          this.data = res.data
          this.organization = this.data.userContactList
          this.organizer = JSON.parse(JSON.stringify(this.data.userContactList))
        })
      },
      saveData () {
        let flag = false
        this.organization.map((v) => {
          if (!v.value || v.value === '') {
            flag = true
            return tools.tip('培训机构对' + v.userName + '学员评分不能为空')
          }
        })
        if (flag) {
          return
        }
        let zbdwFlag = false
        this.organizer.map((v) => {
          if (!v.value || v.value === '') {
            zbdwFlag = true
            return tools.tip('主办单位' + v.userName + '学员评分不能为空')
          }
        })
        if (zbdwFlag) {
          return
        }
        let result = []
        this.organization.map((v) => {
          let kcb = {
            userId: v.userId,
            userName: v.userName,
            classId: v.classId,
            className: this.detail.className,
            score: parseInt(v.value),
            type: '4',
            userContactId: this.detail.id
          }
          result.push(kcb)
        })
        this.organizer.map((v) => {
          let kcb = {
            userId: v.userId,
            userName: v.userName,
            classId: v.classId,
            className: this.detail.className,
            score: parseInt(v.value),
            type: '5',
            userContactId: this.detail.id
          }
          result.push(kcb)
        })
        this.fetchService.trainingplanpjSaveOrUpdateOfAll(result).then(res => {
          if (res.code !== 0) {
            return this.tools.alert(res.message || '获取数据失败')
          }
          this.tools.alert(res.message || '获取数据失败')
          this.tools.back()
        })
      }
    }
  }
</script>

<style scoped>

</style>
